package com.briup.day18;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

/**
 * 数据库信息工具类
 * 补全代码，实现工具类获取配置文件信息
 * @author lining
 * @date 2024/1/30 15:00
 */
public class DbUtils2 {
    //静态成员变量属性 如何赋值？直接赋值  静态代码块赋值
    private static Map<String,String> map;
    static{
        map = new HashMap<>();
        //1.读取配置文件的每一行
        try {
            BufferedReader br = new BufferedReader(new FileReader("db.properties"));
            String line = null;
            while((line = br.readLine())!= null){
                //拆分key=value格式
                if(line.contains("=")){
                    String[] arr = line.split("=");
                    //["username","admin"]
                    if(arr.length == 2){
                        map.put(arr[0],arr[1]);
                    }
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    private DbUtils2(){

    }

    public static String getUserName(){
        return map.get("username");
    }
    public static String getPassword(){
        return map.get("password");
    }
    public static String getUrl(){
        return map.get("url");
    }
    public static String getDriver(){
        return map.get("driver");
    }
}
